package org.wzys.Service.impl;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wzys.Mapper.CarItemMapper;
import org.wzys.Mapper.OrderMapper;
import org.wzys.Pojo.Order;
import org.wzys.Pojo.Review;
import org.wzys.Service.OrderService;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class OrderServiceimpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CarItemMapper carItemMapper;

    //展示订单页面


    @Transactional
    @Override
    //更新订单状态，购物车和订单项删除
    public void updateOrderStatus(String orderId) {

        //更新订单状态，已经支付
        LocalDateTime now = LocalDateTime.now();
        orderMapper.updateOrderStatus(orderId,now);

        //删除购物车项目，通过orderId获得对应订单的productId(List<Integer>)
        //通过List回送给购物车的delete方法,删除购物车项目
        log.info("删除购物车项目");
        List<Integer> productIdList = orderMapper.getProductIdList(orderId);
        for(Integer productId : productIdList){
            carItemMapper.deleteCaritm(productId);
        }
    }

    @Transactional
    @Override
    public void deleteOrder(String orderId) {
        orderMapper.deleteOrderitem(orderId);
        orderMapper.deleteOrder(orderId);
    }

    @Override
    public List<Order> showOrder() {
        return orderMapper.showOrder();
    }

    @Override
    public void addReview(Review review) {
        review.setCreatedTime(LocalDateTime.now());
        orderMapper.addReview(review);
    }

    @Override
    public List<Review> showReview(String orderId) {
        return orderMapper.showReview(orderId);
    }
}
